var express = require('express');
var router = express.Router();

router.use(function(req, res, next){
    if(req.method === "OPTIONS"){
        console.log("OPTIONS!");
        var headers = {};
        headers['Access-Control-Allow-Origin'] = "*";
        headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
        headers["Access-Control-Allow-Credentials"] = false;
        headers["Access-Control-Max-Age"] = "86400";
        headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
        res.writeHead(200, headers);
        res.end();
    }
    else{
        next();
    }
});

/* GET home page. */
router.get('/', function (req, res) {
    res.render('index', { title: 'Express' });
});

var key = "12345566788";


router.post('/login',function(req, res) {
    res.set("Access-Control-Allow-Origin", "*");


    var std_id = req.body.std_id;
    var std_psw = req.body.std_psw;
    var _token_id = req.body._token_id;

    if (std_id == 1234567890) {
        var error = {
            error: "DB!!",
            state: 403,
            target: "std_id"
        };

        return res.status(403).json(error);
    }

    console.log("student id", std_id);
    console.log("student password", std_psw);
    console.log("token", _token_id);

    var data = {
        id : key,
        nextSrc : "/workplace",
        state : 200
    };

    return res.json(data);
});

router.get('/loginout', function(req, res){
    res.set("Access-Control-Allow-Origin", "*");

    var data =  {
        state : "200",
        nextSrc : "/login"
    };

    return res.json(data);

});


router.post("/register", function(req, res){
    res.set("Access-Control-Allow-Origin", "*");

    console.log(req.body);

    var std_name = req.body.std_name;
    var std_id = req.body.std_id;
    var std_number = req.body.std_number;
    var std_department = req.body.std_department;

    console.log("student name", std_name);
    console.log("student id", std_id);
    console.log("student number", std_number);
    console.log("student department", std_department);

    var data = {
        nextSrc : "/login",
        state : 200
    };

    return res.json(data);
});

router.get("/workplace/broadcast/:department", function(req, res){
     res.set("Access-Control-Allow-Origin", "*");

    var department = req.params.department;

    var data = {
        id : "sdsdsdjkqwldlkwqjdoiqjwdo", // 用户认证码
        type : "broadcast",
        department : "Web研发部",
        content : "啦啦啦， 老袁老请吃饭！！！",
        time : "2014-10-01 11:11",
        person : "董小姐"
    };

    return res.json(data);

});

router.get("/workplace/broadcast", function(req, res){
    res.set("Access-Control-Allow-Origin", "*");

    var department = req.params.department;

    var data = {
        "Web研发部" : [
            {
                id : "sdsdsdjkqwldlkwsadsadqjdoiqjwdo",
                type : "broadcast",
                department : "Web研发部",
                content : "啦啦啦， 老袁老请吃饭！！！",
                time : "2014-10-01 11:11",
                person : "董小姐",
                title : "DB董"
            },
            {
                id : "sdsdsdjkqwldl123213kwqjdoiqjwddwwdwdwdwdo",
                type : "broadcast",
                department : "Web研发部",
                content : "啦啦啦， sdsdsdsd！！！",
                time : "2014-10-01 11:11",
                person : "董小qwdwqdwq姐",
                title : "DBwdwd董"
            },
            {
                id : "sdsdsdjkqwldlxzcxzcxckwqjdoiqjwdo23232323",
                type : "broadcast",
                department : "Web研发部",
                content : "啦sasadsadsadsadsadsadsdsd！！！",
                time : "2014-10-01 11:11",
                person : "董小qwdwqdwq姐",
                title : "DBwdwd董"
            }
        ],
        "移动开发部" : [
            {
                id : "sdsdsdjkqwldlkwqjd232323232323oiqjwdo",
                type : "broadcast",
                department : "移动开发部",
                content : "啦啦啦， 老213232323袁老请吃饭！！！",
                time : "2014-10-01 11:11",
                person : "董小姐",
                title : "DB董"
            }
        ]
    };

//
//    var data = [
//        {
//            id : "sdsdsdjkqwldlkwqjd232323232323oiqjwdo",
//            type : "broadcast",
//            department : "Web",
//            content : "啦啦啦， 老213232323袁老请吃饭！！！",
//            time : "2014-10-01 11:11",
//            person : "董小姐",
//            title : "DB董"
//        },
//        {
//            id : "sdsdsdjkqwdwqdwdwqdwldlkwqjd232323232323oiqjwdo",
//            type : "broadcast",
//            department : "Web",
//            content : "啦啦啦， 老wqd213232323袁老请吃饭！！！",
//            time : "2014-10-0wqd1 11:11",
//            person : "董小姐",
//            title : "DB董"
//        },
//        {
//            id : "sdsdsdjkqwldlkxzczxcxzcxzcxzc12323wqjd232323232323oiqjwdo",
//            type : "broadcast",
//            department : "Mobile",
//            content : "啦啦啦， 老213232323袁老请吃饭！！！",
//            time : "2014-10-01 11:11",
//            person : "董小姐",
//            title : "DB董"
//        }
//
//
//    ]

    return res.json(data);

});


router.post('/workplace/broadcastEdit/', function(req, res){
     res.set("Access-Control-Allow-Origin", "*");

    var id = req.body.id;
    var type = req.body.type;
    var content = req.body.content;
    var time = req.body.time;

    console.log("session id", id);
    console.log("type", type);
    console.log("content", content);
    console.log("time", time);
    
    var data = {
        id : "sdsdsdjkqwldlkwqjdoiqjwdo",
        type : "broadcast",
        state : "OK"
    };

    return res.json(data);
});


router.get("/workplace/personal", function(req, res){
 res.set("Access-Control-Allow-Origin", "*");

    var data = {
        "id" : "sdsdsdjkqwldlkwqjdoiqjwdo",
        "std_qq" : "517263494",
        "std_email" : "dongtiangche@gmail.com",
        "type" : "personal",
        "std_id" : "2012214642",
        "std_name" : "董小姐",
        "std_phone" : "18523016284",
        "std_teacher" : "董小姐",
        "std_job" : "干事",
        "std_department" : "Web研发部"    
    };

    return res.json(data);
});

router.post('/workplace/personal', function(req, res){
     res.set("Access-Control-Allow-Origin", "*");

    var id = req.body.id;
    var type = req.body.type;
    var phone = req.body.std_phone;
    var email = req.body.std_email;
    var qq = req.body.std_qq;

    console.log("id", id);
    console.log("type", type);
    console.log("phone", phone);
    console.log("email", email);
    console.log("qq", qq);

    var data = {
        id : id,
        type : type,
        state : 200
    };

    return res.json(data); 
});

router.get("/workplace/mission", function(req, res){
    res.set("Access-Control-Allow-Origin", "*");

    var data = {
        "id" : "sdsdsdjkqwldlkwqjdoiqjwdo",
        "type" : "missionCount",
        "SuccessMission" : "5",
        "RunningMission" : "10"
    };

    return res.json(data);
});


router.get("/mission", function(req, res){
    res.set("Access-Control-Allow-Origin", "*");

    var data = {
        "id" : "sdsdsdjkqwldlkwqjdoiqjwdo",
        "type" : "mission",
        "total" : [
            {
                "state" : "已完成",
                "title" : "Web 后台管理系统",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "99",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！",
                "work_id" : "10"
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统",
                "publisher" : "董小姐",// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd", // 附件名,
                "work_id" : "11",
            },
            {
                "state" : "已完成",
                "title" : "Web qwdwqdwdwqdwq",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "99",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！",
                "work_id" : "12"
            },
            {
                "state" : "未完成",
                "title" : "DB wqdwqddwqdwqdwqdwqd",
                "publisher" : "董小姐",// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd", // 附件名,
                "work_id" : "13"
            },
            {
                "state" : "已完成",
                "title" : "Web 后台管zxxzcc理系统",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "99",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！",
                "work_id" : "14"
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统",
                "publisher" : "董小姐",// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd", // 附件名
                "work_id" : "15"
            },
            {
                "state" : "已完成",
                "title" : "Web 后台12323管理系统",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "99",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！",
                "work_id" : "16"
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统",
                "publisher" : "董小姐",// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd", // 附件名
                "work_id" : "17"
            }
        ],

        "unfinished"  : [{
                "state" : "未完成",
                "title" : "DB 管理系统1",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd", // 附件名
                "work_id" : "14"
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统2",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd", // 附件名
                "work_id" : "15"
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统3",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd", // 附件名
                "work_id" : "12"
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统4",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd", // 附件名
                "work_id" : "15"
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统5",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd", // 附件名
                "work_id" : "19"
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统6",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统7",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统8",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            },
            {
                "state" : "未完成",
                "title" : "DB 管理系统9",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            }
        ],
        "finished" : [
            {
                "state" : "已完成",
                "title" : "Web 后台管理系统",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "99",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！"
            },
            {
                "state" : "已完成",
                "title" : "Web 后台管理系统",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "99",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！"
            },
            {
                "state" : "已完成",
                "title" : "Web 后台管理系统",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "99",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！"
            },
            {
                "state" : "已完成",
                "title" : "Web 后台管理系统",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "99",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！"
            },
            {
                "state" : "已完成",
                "title" : "Web 后台管理系统",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "99",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！"
            },
            {
                "state" : "已完成",
                "title" : "Web 后台管理系统",
                "publisher" : "董小姐", // 发布人,
                "summary" : "全站Ajax。。 坑！",
                "details" : "谁做谁DB， 哦也！",
                "score" : "9239",
                "people" : ["董小姐", "王金鹏", "林艳芳", "刘慧芝", "隆宗亦", "姚蕤", "Java"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "helloworld.psd", // 附件名
                "suggest" : "为毛时间这么短！"
            }
        ],
        "expire" : [
            {
                "state" : "已过期",
                "title" : "DB 管sdsdsdsdsd理系统",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            },
            {
                "state" : "已过期",
                "title" : "DB 管sdsdsdsdsd理系统",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            },
            {
                "state" : "已过期",
                "title" : "DB 管sdsdsdsdsd理系统",
                "publisher" : "董小2323姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            },
            {
                "state" : "已过期",
                "title" : "DB 管sdsdsdsdsd理系统",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            },
            {
                "state" : "已过期",
                "title" : "DB 管sdsdsdsdsd理系统",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            },
            {
                "state" : "已过期",
                "title" : "DB 管sdsdsdsdsd理系统",
                "publisher" : "董小姐" ,// 发布人,
                "summary" : "谁做谁DB",
                "details" : "谁做谁DB， 哦也！",
                "people" : ["Java", "DB"],
                "time" : { begin : "2014-09-30", end : "2014-10-07"},
                "accessory" : "db.psd" // 附件名
            }


        ]

    };

//    var data = {
//        "id" : "sdsdsdjkqwldlkwqjdoiqjwdo",
//        "type" : "mission",
//        "total" : [],
//        "unfinished" : [],
//        "expire" : [],
//        "finished" : []
//    };
    return res.json(data);
});


router.post("/mission/publish", function(req, res){
     res.set("Access-Control-Allow-Origin", "*");

    var id = req.body.id,
        type = req.body.type,
        title = req.body.title,
        summary = req.body.summary,
        details = req.body.details,
        people = req.body.people,
        Endtime = req.body.Endtime,
        accessory = req.body.accessory;

    console.log("id", id);
    console.log("type", type);
    console.log("title", title);
    console.log("summary", summary);
    console.log("details", details);
    console.log("people", people);
    console.log("Endtime",Endtime);
    console.log("accessory", accessory);

    return res.json({
        id : id,
        type : type,
        state : 200
    });
});


router.get('/mission/submit', function(req, res){
     res.set("Access-Control-Allow-Origin", "*");

    var data = {
        "id" : "sdsdsdjkqwldlkwqjdoiqjwdo",
        "type" : "submit",
        "missions" : [
            {
                "title" : "任务1",
                "Endtime" : "2014-10-09",
                "state" : "20" // 20个人提交    
            }
        ]   
    };

    return res.json(data);
});


router.get("/mission/submit/:missions", function(req, res){
     res.set("Access-Control-Allow-Origin", "*");

    var missions = req.params.missions;

    console.log("missions", missions);

    var data = {
        "id" : "sdsdsdjkqwldlkwqjdoiqjwdo",
        "type" : "submit",
        "missionState" : {
            "std_name" : "张三",
            "std_time" : "2014-10-12",
            "score" : "10",
            "suggest" : "这么晚才交！"
        }
    }
});

router.post('/mission/submit/:missions', function(req, res){
     res.set("Access-Control-Allow-Origin", "*");

    var missions = req.params.missions;

    console.log("missions", missions);

    var id = req.body.id;
    var type = req.body.type;
    var missionState = req.body.missionState;

    console.log("id", id);
    console.log("type", type);
    console.log("missionState", missionState);

    var data = {
        "id" : "sdsdsdjkqwldlkwqjdoiqjwdo",
        "type" : "submit",
        "state" : "200"
    }

    return res.json(data);

});

module.exports = router;
